Skip to content

test: add CLI and fetch integration tests (closes #65)#89

Open
dmchaledev wants to merge 1 commit into
mainfrom
claude/nice-mendel-1q6jav
Open

test: add CLI and fetch integration tests (closes #65)#89
dmchaledev wants to merge 1 commit into
mainfrom
claude/nice-mendel-1q6jav

Conversation

@dmchaledev

Copy link
Copy Markdown
Contributor

Summary

Closes #65.

cli.ts and fetch.ts had 0% test coverage despite the CLI's exit-code CI gate being the library's primary use case. This PR adds two test files that cover those modules end-to-end with no new runtime or dev dependencies.

test/cli.test.ts — 12 cases:

  • Exits 0 for A+ sites; exits 1 for D-grade and F-grade sites (the CI gate)
  • --json emits valid JSON with grade, score, headers, and url fields
  • --version / -v prints a semver string
  • --help / -h prints usage text
  • Missing URL writes a usage hint to stderr and exits 1
  • Network errors write the error message to stderr and exit 1
  • --timeout 3000 forwards { timeoutMs: 3000 } to fetchHeaders

test/fetch.test.ts — 7 cases:

  • Response headers are returned as lowercase key-value pairs
  • GET is used instead of HEAD (prevents false-negative CSP omissions)
  • An AbortSignal is wired into the fetch call
  • The response body is cancelled after headers are collected
  • A null body doesn't throw
  • The request is aborted after timeoutMs elapses
  • The abort timer is cleared after a successful fetch (no post-test interference)

src/cli.ts — one-line change: main()await main(). The module's top-level call was unwaited, so a dynamic import('../src/cli.js') would resolve before main() ran, making it impossible to capture output or exit codes in tests. Top-level await in an ESM module (already used by this package: "type": "module", "module": "NodeNext") makes the import wait for main() to settle.


Generated by Claude Code

- Add test/cli.test.ts covering the CI-gate exit-code path (D/F → exit 1,
  A+ → exit 0), --json output, --version/-v, --help/-h, missing-URL error,
  network-error propagation, and --timeout forwarding to fetchHeaders.
- Add test/fetch.test.ts covering header lowercasing, GET-method enforcement,
  AbortSignal wiring, body cancellation, null-body safety, and timeout abort.
- Change cli.ts to use top-level `await main()` so dynamic import() resolves
  only after main() finishes, enabling the vi.resetModules() test pattern.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FvB5dYob9rjdUekNBA4Dvn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CLI integration tests — cli.ts and fetch.ts have 0% coverage, including the exit-code CI-gate feature

2 participants